﻿@{
    Layout = "~/Views/Shared/Index_Layout.cshtml";
}

<div style="margin: 10px 10px 10px 10px">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input placeholder="用户名或单位名称或电话" name="LoginName" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div> 
        </div>
    </form>
</div>

<table class="layui-hide" id="standardtable" lay-filter="standardtableFilter"></table>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">

         <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
         @*<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="delete"> 批量删除 </button>*@

    </div>
</script>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="reset">重置密码</a>
</script>

<script>
    
    layui.use(function() {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            GSLayui = layui.GSLayui;


        var cols = [
            //{ type: "checkbox", width: 50 },
            { title: "序号", width: 60, templet: function(d) { return d.LAY_INDEX; } },
            { field: "LoginName", title: "用户名", minWidth: 100 },
            { field: "Roles", title: "角色", minWidth: 110 },
            { field: "DepartmentName", title: "单位名称", minWidth: 100 },
            { field: "Code", title: "社会信用统一代码", minWidth: 200 },
            { field: "Address", title: "单位地址", minWidth: 100 },
            { field: "Representative", title: "法人", minWidth: 100 },
            { field: "ContactPerson", title: "联系人", minWidth: 100 },
            { field: "ContactPhone", title: "电话", minWidth: 100 },
            { fixed: "right", title: "操作", width: 202, align: "center", toolbar: '#currentTableBar' }

        ];

        var standtable = GSLayui.layuiTable("standardtable", "/SysPerson/DataSysPerson", cols)
        table.render(standtable);

        // 监听搜索操作
        form.on('submit(data-search-btn)', function(data) {

            var tableform = data.field;

            //重载
            table.reload('standardtable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: tableform
            });

            return false;
        });

        // 按钮监听事件
        table.on('toolbar(standardtableFilter)', function(obj) {

            if (obj.event === 'add') {  // 监听添加操作

                window.location.href = "/SysPerson/Edit";

            } else if (obj.event === 'delete') {  // 监听勾选删除

                var checkStatus = table.checkStatus('standardtable')

                    , data = checkStatus.data;
                console.log(data);

            }
        });

        //监听表格复选框选择
        //table.on('checkbox(standardtableFilter)', function(obj) {
        //    console.log(obj)
        //});

        //监听表格按钮
        table.on('tool(standardtableFilter)', function(obj) {

            switch (obj.event) {

                case 'edit':

                    window.location.href = "/SysPerson/Edit?id=" + obj.data.Id;
                    break;
                case 'reset'://弹框，输入框赋默认值，点击确认后，修改密码
                    layer.confirm('重置密码后将无法找回原先的 密码！', function(index) {

                        var data = ajaxPost("/SysPerson/ResetSysPerson/" + obj.data.Id);
                        if (data.code == 200) {
                            layer.alert(data.msg, {
                                title: "成功"
                            });

                            setTimeout(function() {
                                layer.close(layer.index); //自动关闭
                            }, 2000);

                            
                        } else {
                            layer.alert(data.msg, {
                                title: "失败"
                            });
                        }
                    });
                    break;

                case 'delete':
                    layer.confirm('您确认删除？', function(index) {

                        var data = ajaxPost("/SysPerson/DeleteSysPerson/" + obj.data.Id);
                        if (data.code == 200) {
                            layer.alert(data.msg, {
                                title: "成功"
                            });

                            setTimeout(function() {
                                layer.close(layer.index); //自动关闭
                            }, 2000);

                            table.reload('standardtable');

                        } else {
                            layer.alert(data.msg, {
                                title: "失败"
                            });
                        }
                    });
                    break;

            };
        });



    });

</script>